package com.example.multidrive;

import com.example.multidrive.R.id;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class EditarNota extends Activity {

	DatabaseSQL multidriveDb = null;
	SQLiteDatabase db = null;

	String disciplina;
	String titulo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_editar_nota);

		getLista();
	}

	protected void onResume() {
		super.onResume();
		getLista();
	}

	public void atualizarNota(View view) {

		try {
			EditText txtTitulo = (EditText) findViewById(id.editTextTituloEdit);
			String titulo = txtTitulo.getText().toString();
			EditText txtCorpo = (EditText) findViewById(id.editTextCorpoEdit);
			String corpo = txtCorpo.getText().toString();

			multidriveDb = new DatabaseSQL(getApplicationContext());
			db = multidriveDb.getWritableDatabase();

			db.execSQL("UPDATE notas SET titulo = '" + titulo + "',"
					+ "texto = '" + corpo + "'");

			Toast.makeText(getApplicationContext(), "Nota alterada com sucesso!" , Toast.LENGTH_LONG).show();

			db.close();
			multidriveDb.close();

			Intent i = new Intent(getApplicationContext(), MainActivity.class);
			startActivity(i);

		} catch (Exception ex) {
			new AlertDialog.Builder(this).setMessage(ex.getMessage())
					.setPositiveButton("ERRO", null).show();
		}
	}

	public void getLista() {
		try {
			multidriveDb = new DatabaseSQL(getApplicationContext());
			atualizarLista();
		} catch (Exception e) {
			new AlertDialog.Builder(this).setMessage(e.getMessage())
					.setPositiveButton("OK", null).show();
		}
	}

	protected void atualizarLista() {
		db = multidriveDb.getReadableDatabase();

		Intent intent = getIntent();
		disciplina = intent.getStringExtra("nomeDisciplina");
		titulo = intent.getStringExtra("tituloNota");

		Cursor cursor = db.rawQuery(
				"SELECT * FROM notas WHERE nomeDisciplina = '" + disciplina
						+ "' AND titulo = '" + titulo + "'", null);
		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {
			String tituloEdit = cursor.getString(1);
			String corpoEdit = cursor.getString(2);

			EditText tituloEd = (EditText) findViewById(id.editTextTituloEdit);
			tituloEd.setText(tituloEdit);

			EditText corpoEd = (EditText) findViewById(id.editTextCorpoEdit);
			corpoEd.setText(corpoEdit);

			cursor.moveToNext();
		}
		cursor.close();
		db.close();
		multidriveDb.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.editar_nota, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
